package mobi.greenplate.theweb.domain;

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import java.util.Date;
import mobi.greenplate.theweb.persistence.UnonwedLoader;

@Entity
public class RequestRide implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    @Enumerated(EnumType.STRING)
    private RideState rideState;
    //ManyToOne - unowned
    @Basic
    private Long requesterId;
    @Transient
    private User requester;
    //ManyToOne - unowned
    @Basic
    private Long startAddressId;
    @Transient
    private Address startAddress;
    //ManyToOne - unowned
    @Basic
    private Long endAddressId;
    @Transient
    private Address endAddress;
    @Temporal(TemporalType.DATE)
    private Date startTime;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public String toString() {
        return this.getClass().getName() + "[id=" + this.getId() + "]";
    }

    public RideState getRideState() {
        return rideState;
    }

    public void setRideState(RideState rideState) {
        this.rideState = rideState;
    }

    public User getRequester() {
        if (null == requesterId) {
            return null;
        }
        if (null != requester && requester.getId().equals(requesterId)) {
            return requester;
        }
        requester = UnonwedLoader.loadById(User.class, requesterId);
        return requester;
    }

    public void setRequester(User requester) {
        this.requesterId = requester.getId();
    }

    public Address getStartAddress() {
        if (null == startAddressId) {
            return null;
        }
        if (null != startAddress && startAddress.getId().equals(startAddressId)) {
            return startAddress;
        }
        startAddress = UnonwedLoader.loadById(Address.class, startAddressId);
        return startAddress;
    }

    public void setStartAddress(Address startAddress) {
        this.startAddressId = startAddress.getId();
    }

    public Address getEndAddress() {
        if (null == endAddressId) {
            return null;
        }
        if (null != endAddress && endAddress.getId().equals(endAddressId)) {
            return endAddress;
        }
        endAddress = UnonwedLoader.loadById(Address.class, endAddressId);
        return endAddress;
    }

    public void setEndAddress(Address endAddress) {
        this.endAddressId = endAddress.getId();
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }
}
